package com.gistone.commis.api.restController;

import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.entity.PropertyType;
import com.gistone.commis.mapper.PropertyTypeMapper;
import com.gistone.commis.util.DataTablePageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangfan on 2017/5/5.
 */
@RestController("apiPropertyTypeController")
@RequestMapping("api/propertyType")
public class PropertyTypeController {

    private static final Logger logger = LoggerFactory.getLogger(PropertyTypeController.class);


    @Autowired
    private PropertyTypeMapper propertyTypeMapper;




    /**
     * 获取所有资产类型
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/all")
    public List<PropertyType> loadAll() {
        return propertyTypeMapper.findPropertyTypes();
    }



    /**
     * 新建资产类型信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public Object create(HttpServletRequest request, HttpServletResponse response) {


        PropertyType propertyType = new PropertyType();
        EditorResult<PropertyType> propertyTypeEditorResult = new EditorResult<>();
        propertyType.setTypeName(request.getParameter("data[0][typeName]"));

        if (propertyTypeMapper.insertSelective(propertyType)> 0) {
            List<PropertyType> data = new ArrayList<>();
            data.add(propertyTypeMapper.selectByPrimaryKey(propertyType.getTypeId()))	;
            propertyTypeEditorResult.setData(data);
        } else {
            response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }


        return propertyTypeEditorResult;

    }

    /**
     * 批量修改资产类型信息， id以,号分割
     *
     * @param houseIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.PUT, value = "/edit")
    public Object edit(@RequestParam("id") String houseIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = houseIdStr.split(",");
        EditorResult<PropertyType> propertyTypeEditorResult = new EditorResult<>();

        List<PropertyType> data =new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            int propertyTypeId = Integer.valueOf(arr[i]);

            System.out.println(propertyTypeId);
            PropertyType propertyType = new PropertyType();

            propertyType.setTypeId(propertyTypeId);
            propertyType.setTypeName(request.getParameter("data["+propertyTypeId+"][typeName]"));


            if (propertyTypeMapper.updateByPrimaryKeySelective(propertyType) > 0) {
                data.add(propertyTypeMapper.selectByPrimaryKey(propertyTypeId));


            }else{
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }


        }

        propertyTypeEditorResult.setData(data);
        return propertyTypeEditorResult;


    }


    /**
     * 用户删除
     * 通过房屋ID批量删除房屋信息， id以,号分割
     *
     * @param propertyTypeIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.DELETE, value = "/remove")
    public Object remove(@RequestParam("id") String propertyTypeIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = propertyTypeIdStr.split(",");
        EditorResult<PropertyType> propertyTypeEditorResult = new EditorResult<>();
        for (int i = 0; i < arr.length; i++) {
            int propertyTypeId = Integer.valueOf(arr[i]);
            if (propertyTypeMapper.deleteByPrimaryKey(propertyTypeId) > 0) {
                System.out.println("删除" + propertyTypeId + "成功！");


            } else {
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }
        }
        return propertyTypeEditorResult;


    }

    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<PropertyType> pageDatatables(HttpServletRequest request, PropertyType propertyType) {
//		HttpSession httpSession = request.getSession();
//		PropertyType propertyType = (PropertyType) httpSession.getAttribute("propertyTypeInfo");
//		logger.info("用户信息：" + propertyType.toString());
//		int propertyType_id = propertyType.getPropertyType().getPropertyTypeId();
//		propertyType.setPropertyTypeId(propertyType_id);
        System.out.println(propertyType.toString());

        List<PropertyType> communities =propertyTypeMapper.findPropertyTypes();


        DataTablePageUtil<PropertyType> dataTable = new DataTablePageUtil<>(request);

        PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());
        List<PropertyType> list = propertyTypeMapper.selectSelective(propertyType);
        PageInfo<PropertyType> pageInfo =new PageInfo<>(list);
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面


        return dataTable;

    }


}
